<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
    //分类添加
    static public function getCate(){
        $data=DB::table('cate')->select(DB::raw('*,concat(path,cid) as paths'))->orderBy('paths')->get();
        
        //便利数据  把分类的名字前加入相应的符号
        foreach($data as $k=>$v){
            $l=substr_count($v['path'],',');
            $data[$k]['cname']=str_repeat('|----',$l-1).$v['cname'];

        }
       // dd($data);
        return $data;
    }

    //显示添加分类页面
    public function getAdd($cid=''){
        // dd(self::getCate());
        
        $data=self::getCate();
        return view('/admin/cate/add',['data'=>$data,'cid'=>$cid]);
    }

    //处理添加分类操作
    public function postInsert(Request $request){
        $data=array();
        $data=$request->except('_token');
        
        if($data['pid']==0){
            // dd($data);
            $data['path']=$data['pid'].',';
        }else{
            $info=DB::table('cate')->where('cid','=',$data['pid'])->first();
             //dd($info);
            $data['path']=$info['path'].$data['pid'].',';
        }
        $res=DB::table('cate')->insert($data);

        if($res>0){
            return redirect('/cate/index')->with('success','插入成功');
        }else{
            return back()->with('error','插入失败');
        }
    }

    //显示列表页
    public function getIndex(Request $request){
        // $data=self::getCate1();
        // dd($data);
                //获取搜索id
        $search = $request->input('search');

        //获取页数id
        $num = $request->input('num',10);

         if(empty($search)){
            $data=DB::table('cate')->select(DB::raw('*,concat(path,cid) as paths'))->orderBy('paths')->paginate($num);            
        }else{
            //获取列表页信息
            $data=DB::table('cate')->select(DB::raw('*,concat(path,cid) as paths'))->where('cname',$search)->orderBy('paths')->paginate($num);          
        }


        //便利数据  把分类的名字前加入相应的符号
        // foreach($data as $k=>$v){
        //     $l=substr_count($v['path'],',');
        //     $data[$k]['cname']=str_repeat('|----',$l-1).$v['cname'];

        // }
        // dd($data);
        return view('/admin/cate/index',['data'=>$data,'request'=>$request->all()]);
    }

    //进行删除操作
    public function getDelete($cid){

        $count=DB::table('cate')->where('pid','=',$cid)->count();
        if($count>0){
            return back()->with('error','您删除的分类下面有子类');
        }else{
            $res=DB::table('cate')->where('cid','=',$cid)->delete();
                if($res>0){
                    return redirect('/cate/index')->with('success','删除成功');
                }else{
                    return back()->with('error','删除失败');
                }
        }
    }
    //显示修改页面
    public function getEdit($cid){
        $data=DB::table('cate')->where('cid','=',$cid)->first();
        // dd($data);
        return view('/admin/cate/edit',['data'=>$data]);
    }
    //修改操作
    public function postUpdate($cid,Request $request){
        $data=$request->except('_token');
        // dd($data);
        $res=DB::table('cate')->where('cid','=',$cid)->update($data);
        if($res>0){
            return redirect('/cate/index')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }
}